package com.jmu.content.controller.admin;


import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jmu.constant.MessageConstant;
import com.jmu.content.dto.ChargePilePageDTO;
import com.jmu.content.pojo.ChargePile;
import com.jmu.content.pojo.ChargeStation;
import com.jmu.content.result.PageResult;
import com.jmu.content.result.Result;
import com.jmu.content.service.ChargePileService;
import com.jmu.content.service.ChargeStationService;
import com.jmu.content.vo.ChargePileAdminVO;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fems
 * @since 2024-05-06
 */
@RestController("adminChargePileController")
@RequestMapping("/admin/charge-pile")
public class ChargePileController {

    @Autowired
    ChargePileService chargePileService;

    @Autowired
    ChargeStationService chargeStationService;

    @GetMapping("/page")
    @ApiOperation(value = "分页充电桩信息")
    public Result<PageResult> page(ChargePilePageDTO dto){
        PageResult pageResult = chargePileService.pageQuery(dto);
        return Result.success(pageResult);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查找充电桩")
    public Result<ChargePile> getById(@PathVariable Long id){
        ChargePile chargePile = chargePileService.getById(id);
        ChargePileAdminVO chargePileVO = new ChargePileAdminVO();
        BeanUtils.copyProperties(chargePile,chargePileVO);
        ChargeStation station = chargeStationService.getById(chargePile.getStationId());
        chargePileVO.setStationName(station.getName());
        if (chargePile == null){
            return Result.error(MessageConstant.CHARGE_PILE_NOT_FOUND);
        }
        return Result.success(chargePileVO);
    }

    @PostMapping()
    @ApiOperation("添加充电桩")
    public Result add(@RequestBody ChargePile chargePile){
        chargePileService.save(chargePile);
        return Result.success();
    }

    @PutMapping
    @ApiOperation("修改充电桩信息")
    public Result update(@RequestBody ChargePile chargePile){

        LambdaUpdateWrapper<ChargePile> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ChargePile::getId,chargePile.getId())
                .set(chargePile.getCount()!=null,ChargePile::getCount,chargePile.getId())
                .set(chargePile.getStationId()!=null,ChargePile::getStationId,chargePile.getStationId())
                .set(chargePile.getStatus()!=null,ChargePile::getStatus,chargePile.getStatus())
                .set(chargePile.getCurrent()!=null,ChargePile::getCurrent,chargePile.getCurrent())
                .set(chargePile.getVoltage()!=null,ChargePile::getVoltage,chargePile.getVoltage())
                .set(chargePile.getMaxPower()!=null,ChargePile::getMaxPower,chargePile.getMaxPower())
                .set(chargePile.getTotalPower()!=null,ChargePile::getTotalPower,chargePile.getTotalPower())
                .set(chargePile.getIsLocked()!=null,ChargePile::getIsLocked,chargePile.getIsLocked());

        chargePileService.update(updateWrapper);

        return Result.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除充电桩")
    public Result delete(@PathVariable Long id){
        boolean bool = chargePileService.removeById(id);
        if (!bool){
            return Result.error(MessageConstant.CHARGE_PILE_NOT_FOUND);
        }
        return Result.success();
    }



}

